package cn.datawin.cmppclient.sender.queue;

import cn.datawin.sms.cmpp2.SmsDetailLog;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

@Slf4j
public class QueueCache {

    public static ConcurrentHashMap<String, ConcurrentLinkedQueue<SmsDetailLog>> MESSAGES_QUEUE = new ConcurrentHashMap<>();

//    public static void clearCache(String channelNo) {
//        if (!MESSAGES_QUEUE.containsKey(channelNo)) {
//            //updateProvider(channelNo);
//            return;
//        }
//        ConcurrentLinkedQueue<SubmitLog> submitLogs = MESSAGES_QUEUE.get(channelNo);
//        if (submitLogs.size() == 0) {
//            //updateProvider(channelNo);
//            return;
//        }
//        SubmitLog submit = submitLogs.poll();
//        while (submit != null) {
//
//            ReportLog reportLog = new ReportLog();
//            reportLog.setCrMsgId(submit.getCrMsgId());
//            reportLog.setStat("A:" + _40001.getStatus());
//            reportLog.setSubmitTime(new DateTime(submit.getSubmitDate()).toString("yyMMddHHmm"));
//            reportLog.setDoneTime(new DateTime().toString("yyMMddHHmm"));
//            reportLog.setPhoneNo(submit.getPhoneNo());
//            reportLog.setCreateDate(new Date());
//            reportLog.setProviderNo(submit.getProviderId());
//            reportLog.setCrAccount(submit.getAccount());
////            log.debug("reportLog={}", JSON.toJSONString(reportLog));
//            SpringContextUtil.getBean(ProducerService.class).sendMessage(RocketMqCommonTopic.SMS_CMPP_REPORT_TOPIC, JSON.toJSONString(reportLog));
//
//            if (getConnections(channelNo) > 0) {
//                break;
//            }
//
//            submit = submitLogs.poll();
//        }
//
//        //更新供应商状态
//        //updateProvider(channelNo);
//
//    }
//
//    public static void alertProvider(String channelNo, Integer status) {
////        ResProviderService resProviderService = SpringContextUtil.getBean(ResProviderService.class);
////        ResProvider resProvider = new ResProvider();
////        resProvider.setProviderId(providerId);
////        resProvider.setStatus(0);
////        resProviderService.updateById(resProvider);
//        //更新缓存
////        ResProviderChannelService resProviderChannelService = SpringContextUtil.getBean(ResProviderChannelService.class);
////        resProviderChannelService.dataToCache(providerId);
//
//        Long providerId = Long.valueOf(channelNo);
//        ProducerService producerService = SpringContextUtil.getBean(ProducerService.class);
//        ProviderErrorLog providerErrorLog = new ProviderErrorLog();
//        providerErrorLog.setCreateDate(new Date());
//        providerErrorLog.setProviderId(providerId);
//        providerErrorLog.setStatus(status);
//        producerService.sendMessage(RocketMqCommonTopic.SMS_CMPP_PROVIDER_ERROR_TOPIC, JSON.toJSONString(providerErrorLog));
//    }
//
//    public static int getConnections(String providerNo) {
//        AbstractClientSessionManager clientManager = NetwayEnv.getClientManager(providerNo);
//        int connectedNum = 0;
//        if (clientManager != null) {
//            connectedNum = clientManager.getSessionSize();
//        }
//
//        return connectedNum;
//    }

}
